package com.uyayo.framework.pagehelper.pojo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Pagination<T> {
	public static long DEFAULT_PAGEROWS = 10;
	
	private long page = 1;// 当前页
	private long totalPage;//总页数  
    private long total = 0;  //总记录数
	private List<T> rows = new ArrayList<T>();  //集合数据
	private long pageRows = 20;   //每页显示记录数
	private Map<String, Object> params = new HashMap<String, Object>();//其他的参数我们把它封装成一个Map对象  
	
	private String sort ="";
	private String order="";

	private boolean isNotPage = true; 
	
	public boolean isNotPage() {
		return isNotPage;
	}
	public void setNotPage(boolean isNotPage) {
		this.isNotPage = isNotPage;
	}
	public Pagination() {
		super();
	}
	public Pagination(long totalPage, long pageRows, long total) {
		 this.totalPage = computePageNumber(totalPage, this.pageRows, this.total);
		 this.pageRows = pageRows;
		 this.total = (long) total;
	}
	private static long computeLastPageNumber(long total, long pageRows) {
	    if (pageRows <= 0){
	    	return 1;
	    }
	    long totalPage = total % pageRows == 0 ? total / pageRows :  total / pageRows + 1;
	    if (totalPage <= 1){
	    	totalPage = 1;
	    }
	    return totalPage;
	  }

	  private static long computePageNumber(long totalPage, long pageRows, long total) {
	    if (totalPage <= 1) {
	      return 1;
	    }
	    if ((2147483647 == totalPage) || (totalPage > computeLastPageNumber(total, pageRows)))
	    {
	      return computeLastPageNumber(total, pageRows);
	    }
	    return totalPage;
	  }
	  
	public Long getPage() {
		return page;
	}

	public void setPage(long page) {
		this.page = page;
	}

	

	public long getTotal() {
		return total;
	} 
	   
	public void setTotal(long total) {
		this.total = total;
		 //在设置总页数的时候计算出对应的总页数，在下面的三目运算中加法拥有更高的优先级，所以最后可以不加括号。  
	     long totalPage = total%pageRows==0 ? total/pageRows : total/pageRows + 1;  
	     this.setTotalPage(totalPage);  
	}
    
    public List<T> getRows() {
		return rows;
	}

	public void setRows(List<T> rows) {
		this.rows = rows;
	}
    
    public long getTotalPage() {  
       return totalPage;  
    }  
   
    public void setTotalPage(long totalPage) {  
       this.totalPage = totalPage;  
    }  
   
    public long getPageRows() {
		return pageRows;
	}
	public void setPageRows(long pageRows) {
		this.pageRows = pageRows;
	}
	
    
    public Map<String, Object> getParams() {  
       return params;  
    }  
     
    public void setParams(Map<String, Object> params) {  
       this.params = params;  
    }  
   
    
    public String getSort() {
		return sort;
	}
	public void setSort(String sort) {
		this.sort = sort;
	}
	public String getOrder() {
		return order;
	}
	public void setOrder(String order) {
		this.order = order;
	}
	
   
    @Override
	public String toString() {
		return "Pager [page=" + page + ", totalPage=" + totalPage + ", total="
				+ total + ", rows=" + rows + ", pageRows=" + pageRows
				+ ", params=" + params + ", sort=" + sort + ", order=" + order
				+ "]";
	}
} 
